﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class BorrowBooks_form : Form
    {
        public BorrowBooks_form()
        {
            InitializeComponent();
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            string bookid = textBox1.Text;
            if(bookid.Length == 0 )
            {
                return;
            }
            string sql = $"select bookname from BookInfo where bookid = '{bookid}'";
            DataRow dr = DBhelper.GetDataRow(sql);
            if(dr != null)
            {
                textBox2.Text = dr["bookname"].ToString();
            }
            else
            {
                MessageBox.Show("不存在此书籍");
                textBox1.Focus();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string stuid = textBox3.Text;
            if (stuid.Length == 0)
            {
                MessageBox.Show("请输入学号");
                textBox3 .Focus();
                return;
            }
            if (!DBhelper.checkStu(stuid))
            {
                MessageBox.Show("该学号不正确或未注册");
                textBox1.Focus();
                return;
            }
            string pwd = textBox4.Text;
            if (pwd.Length == 0)
            {
                MessageBox.Show("请输入密码");
                textBox4.Focus();
                return;
            }
            if(!DBhelper.checkStu(stuid,pwd))
            {
                MessageBox.Show("账号或密码不正确");
                textBox4.Focus();
                return;
            }

            string bookid = textBox1.Text.Trim();
            //修改图书已借阅数量
            string sql = $"update BookInfo set useamount = useamount+1 where bookid = '{bookid}'";
            bool a = DBhelper.NonQuery(sql);
            //添加借阅信息
            string sql2 = $"insert into RetrieveBook values('{stuid}','{bookid}',default,null,null)";
            bool b = DBhelper.NonQuery(sql2);
            if(a&&b)
            {
                MessageBox.Show("借阅成功！");
            }
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
        }

        private void BorrowBooks_form_Load(object sender, EventArgs e)
        {
            textBox5.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }
}
